package com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.ViewGroup;

import androidx.cardview.widget.CardView;

import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyle;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;

//CardView继承自Framelayout.如果要自定义布局,自己再用一个Layout套着.
public abstract class JsMenuItemViewB extends CardView {

    public JsMenuItemVm mvm;
    public JsMenuItemVm mvm_selected;
    public JsMenuItemVm mvm_unselected;

    public JsMenuItemViewB(Context context) {
        super(context);
    }



    //用于安装ui,layout,安装事件
    public void setVm(JsMenuItemVm currentVm) {

        Context context = getContext();
        mvm = currentVm;
        JsMenuItemStyle style = currentVm.currentStyle();
        Object model = currentVm.currentModel();

        //基类的通用更新
        //CardView的属性设置
        setRadius(style.mi_cornerRadius);
        //z轴的阴影效果.
        setElevation(style.mf_elevation);
////        setElevation(0);
//        setCardElevation(currentVm.mf_elevation);
        //更新ItemView的背景色
        if(style.mI_backgroundColor != null) {
            //这一层的背景色,圆角效果没有.
            //setBackgroundColor(currentVm.mI_backgroundColor);
            //这个是设置cardView的背景色,圆角起效的是这一层.
            setCardBackgroundColor(style.mI_backgroundColor);
        }

        customSetVm(context, currentVm);
        //GaMark: 目前的更新size的操作,在onBindViewHolder时处理,在setupUi这里处理,为什么会导致获取不到???
        //这里不能重新设置lp,但是可以修改已经有的lp
//        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(currentVm.mi_width,currentVm.mi_height);
//        setLayoutParams(lp);
        ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams)getLayoutParams();
        if(lp != null) {
            lp.width = style.mi_width;
            lp.height = style.mi_height;
        }

        //触发onDraw
        invalidate();
    }

    /**
     * 子类显示ui的函数,会重复调用.根据itemVm来显示内容.
     * @param context
     * @param itemVm
     */
    public abstract  void customSetVm(Context context, JsMenuItemVm itemVm);

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        int strokeWidth = mvm.currentStyle().mi_borderWidth;

//        if(mvm.mI_backgroundColor != null) {
//            Paint paint = new Paint();
//            paint.setColor(mvm.mI_backgroundColor);
//            paint.setStyle(Paint.Style.FILL);
//            canvas.drawRoundRect(strokeWidth / 2, strokeWidth / 2, getMeasuredWidth() - strokeWidth / 2, getMeasuredHeight() - strokeWidth / 2, mvm.mi_cornerRadius, mvm.mi_cornerRadius, paint);
//        }

        if(strokeWidth > 0 && mvm.currentStyle().mI_borderColor != null) {
            Paint paint = new Paint();
            paint.setColor(mvm.currentStyle().mI_borderColor);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(strokeWidth);
            int width = getMeasuredWidth();
            int height = getMeasuredHeight();
            canvas.drawRoundRect(strokeWidth / 2, strokeWidth / 2, getMeasuredWidth() - strokeWidth / 2, getMeasuredHeight() - strokeWidth / 2, mvm.currentStyle().mi_cornerRadius,mvm.currentStyle().mi_cornerRadius, paint);
        }
    }
//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        Paint paint = new Paint();
//        paint.setColor(Color.argb(255,0,0,0));
//        paint.setStyle(Paint.Style.STROKE);
//        paint.setStrokeWidth(20);
//        int width = getMeasuredWidth();
//        int height = getMeasuredHeight();
//        JsLogManager.logc_d("JsMenuItemView/onDraw","width is %d,height is %d",width,height);
//        canvas.drawRoundRect(0,0,width,height,20,20,paint);
//    }
}
